const JWT = require('jsonwebtoken');
const {sercet} = require('../config/config');

module.exports = checkTokenMiddleware = (req, res, next) => {
  let token = req.get('token');
  if(!token){
    return res.json({
      code: '2003',
      msg: '未登录',
      data:  null
    })
  }
  JWT.verify(token, sercet, (err, data) => {
    if (!data){
      return res.json({
          code:'2002',
          msg: '数据库读取失败1',
          data: null
      })
    }
    if(err){
      return res.json({
        code: '2004',
        msg: 'token 校验失败',
        data:  null
      })
    }
    req.user = data;

    next();
    });
}